package com.david.appletree

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.david.core.imgloader.LruImageLoader
import com.github.chrisbanes.photoview.PhotoView

class BigImgPop(context: Context):PopupWindow(context) {
    val bigImgs = ArrayList<String>()

    init {

        contentView = LayoutInflater.from(context)!!.inflate(R.layout.pop_big_img, null)

        // 去掉默认的动画效果(showAsDropDown可能会自带默认动画)
//        setAnimationStyle(R.style.custom_anim_pop);
        // 下面两行是为了让PopupWindow能够相应返回按键
        isFocusable = true;
        setBackgroundDrawable(ColorDrawable(0x000000))
        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT

        bigImgs.add("https://s.cn.bing.net/th?id=OIP-C.kB-Ovasi0GW67-rmwnAcwAHaEo&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=2&pid=3.1&rm=2")
        bigImgs.add("https://foter.com/photos/420/7788268.jpg?s=cov3")
        bigImgs.add("https://img-blog.csdn.net/20170329175929604?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanhxMTk5NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast")
        bigImgs.add("https://img-blog.csdn.net/20170329175929604?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanhxMTk5NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast")
        bigImgs.add("https://img-blog.csdn.net/20170329175929604?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanhxMTk5NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast")


        val recyclerView = contentView.findViewById<RecyclerView>(R.id.pop_big_imgs)
        recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        PagerSnapHelper().attachToRecyclerView(recyclerView)

        recyclerView.adapter = BigRecyclerAdapter()
    }


    inner class BigRecyclerAdapter: RecyclerView.Adapter<BigRecyclerHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BigRecyclerHolder {
            val view = PhotoView(parent.context)
            view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
            return BigRecyclerHolder(view)
        }

        override fun onBindViewHolder(holder: BigRecyclerHolder, position: Int) {
            LruImageLoader.getInstance().loadCacheImage(
                bigImgs[position],
                holder.img
            )
        }

        override fun getItemCount(): Int {
            return bigImgs.size
        }

    }

    inner class BigRecyclerHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        var img = itemView as ImageView
    }
}